﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace DingTalkAgent.Web.HtmlHelpers
{
    public static class PagingHelpers
    {
        /// <summary>
        /// 扩展分页方法
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pagingInfo">页面信息</param>
        /// <param name="pageUrl">页面连接地址</param>
        /// <returns>分页字符窜</returns>
        public static MvcHtmlString PageLinks(this HtmlHelper html,
                      Models.PagingInfo pagingInfo,
                      Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<table class=\"Li_Page\"><tr>\r\n");
            //首页
            if (pagingInfo.page == 1)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\">首页</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(1) + "\">首页</a>");
                result.Append("</td>");
            }
            //上一页
            if (pagingInfo.page == 1)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\" >上一页</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.page - 1) + "\">上一页</a>");
                result.Append("</td>");
            }
            for (int i = 1; i <= pagingInfo.total; i++)
            {
                //内容页  总的要存在9页

                if (pagingInfo.total - pagingInfo.page > 8)
                {
                    if (i == pagingInfo.total - 2)
                    {
                        result.Append("<td class=\"Li_Page_btnP\">");
                        result.Append("<a href=\"#\">…</a>");
                        result.Append("</td>");
                        continue;
                    }
                    if (i >= pagingInfo.page && (i - pagingInfo.page < 8 || pagingInfo.total - 2 < i))
                    {
                        result.Append("<td class=\"Li_Page_btnP\">");
                        result.Append("<a href=\"#\" onclick=\"" + pageUrl(i) + "\"");
                        if (i == pagingInfo.page)
                            result.Append(" class=\"selected\"");
                        result.Append(">" + i + "</a>");
                        result.Append("</td>");
                    }
                }
                else
                {
                    if (pagingInfo.total - i < 9)
                    {
                        result.Append("<td class=\"Li_Page_btnP\">");
                        result.Append("<a href=\"#\" onclick=\"" + pageUrl(i) + "\"");
                        if (i == pagingInfo.page)
                            result.Append(" class=\"selected\"");
                        result.Append(">" + i + "</a>");
                        result.Append("</td>");
                    }
                }
            }
            //下一页
            if (pagingInfo.page == pagingInfo.total)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\" >下一页</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.page + 1) + "\">下一页</a>");
                result.Append("</td>");
            }
            //尾页
            if (pagingInfo.page == pagingInfo.total)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\">尾页</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.total) + "\">尾页</a>");
                result.Append("</td>");
            }
            //总记录数Li_Page_span
            result.Append("<td class=\"Li_Page_span\">");
            result.Append("共<span>" + pagingInfo.total + "</span>条记录");
            result.Append("</td>");

            result.Append("\r\n</tr></table>");
            return MvcHtmlString.Create(result.ToString());
        }
        /// <summary>
        /// 扩展分页方法
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pagingInfo">页面信息</param>
        /// <param name="pageUrl">页面连接地址</param>
        /// <returns>分页字符窜</returns>
        public static MvcHtmlString PageLinks2(this HtmlHelper html,
                      Models.PagingInfo pagingInfo,
                      Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<table class=\"Li_Page\"><tr>\r\n");
            result.Append("<td class=\"Li_Page_btn\" >");
            //首页
            if (pagingInfo.page == 1)
            {
                result.Append("<a href=\"#\" class=\"first\">首页</a>");
            }
            else
            {
         
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(1) + "\" class=\"first\">首页</a>");
    
            }
            //上一页
            if (pagingInfo.page == 1)
            {
              
                result.Append("<a href=\"#\" class=\"front\"></a>");
               
            }
            else
            {
                
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.page - 1) + "\" class=\"front\"></a>");
               
            }
            for (int i = 1; i <= pagingInfo.total; i++)
            {
                //内容页  总的要存在9页

                if (pagingInfo.total - pagingInfo.page > 8)
                {
                    if (i == pagingInfo.total - 2)
                    {

                        result.Append("<a href=\"#\" class\"page_link\">…</a>");
               
                        continue;
                    }
                    if (i >= pagingInfo.page && (i - pagingInfo.page < 8 || pagingInfo.total - 2<i))
                    {
                        
                        result.Append("<a href=\"#\" onclick=\"" + pageUrl(i) + "\"");
                        if (i == pagingInfo.page)
                            result.Append(" class=\"page_link selected\"");
                        else {
                            result.Append(" class=\"page_link\"");
                        }
                        result.Append(">" + i + "</a>");
                   
                    }
                }
                else
                {
                    if (pagingInfo.total-i<9)
                    {
                       
                        result.Append("<a href=\"#\" onclick=\"" + pageUrl(i) + "\"");
                        if (i == pagingInfo.page)
                            result.Append(" class=\"page_link selected\"");
                        else
                        {
                            result.Append(" class=\"page_link\"");
                        }
                        result.Append(">" + i + "</a>");
                  
                    }
                }
            }
            //下一页
            if (pagingInfo.page == pagingInfo.total)
            {
            
                result.Append("<a href=\"#\" class=\"next\" ></a>");
              
            }
            else
            {
               
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.page + 1) + "\" class=\"next\" ></a>");
             
            }
            //尾页
            if (pagingInfo.page == pagingInfo.total)
            {
         
                result.Append("<a href=\"#\" class=\"end\" >尾页</a>");
             
            }
            else
            {
              
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.total) + "\" class=\"end\" >尾页</a>");
             
            }
            //跳转
            result.Append(" <label class=\"mypage_label\">");
            result.Append("跳转至<input type='text' value='" + pagingInfo.page + "' class='MyPage' id='MyPage' name='MyPage'/>页");
            result.Append("</label>");

            //总记录数Li_Page_span
            result.Append("<label>");
            result.Append("共<span>" + pagingInfo.total + "</span>条记录");
            result.Append("</label>");

            result.Append("\r\n</td></tr></table>");
            return MvcHtmlString.Create(result.ToString());
        }
        /// <summary>
        /// 扩展分页方法
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pagingInfo">页面信息</param>
        /// <param name="pageUrl">页面连接地址</param>
        /// <returns>分页字符窜</returns>
        public static MvcHtmlString PageLinks3(this HtmlHelper html,
                      Models.PagingInfo pagingInfo,
                      Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<table class=\"Li_Page\"><tr>\r\n");
            //首页
            if (pagingInfo.page == 1)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\">首</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(1) + "\">首</a>");
                result.Append("</td>");
            }
            //上一页
            if (pagingInfo.page == 1)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\" >上</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.page - 1) + "\">上</a>");
                result.Append("</td>");
            }
            
            //下一页
            if (pagingInfo.page == pagingInfo.total)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\" >下</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.page + 1) + "\">下</a>");
                result.Append("</td>");
            }
            //尾页
            if (pagingInfo.page == pagingInfo.total)
            {
                result.Append("<td class=\"Li_Page_btn\" disabled=\"disabled\">");
                result.Append("<a href=\"#\">尾</a>");
                result.Append("</td>");
            }
            else
            {
                result.Append("<td class=\"Li_Page_btn\">");
                result.Append("<a href=\"#\" onclick=\"" + pageUrl(pagingInfo.total) + "\">尾</a>");
                result.Append("</td>");
            }
            //总记录数Li_Page_span
            result.Append("<td class=\"Li_Page_span\">");
            result.Append("共<span>" + pagingInfo.total + "<span>条");
            result.Append("</td>");

            result.Append("\r\n</tr></table>");
            return MvcHtmlString.Create(result.ToString());
        }
       
    }

}